<?php

namespace App\Http\Middleware;

use Closure;
use App\Libs\Weixin;

//use Illuminate\Contracts\Auth\Guard;

class CheckUserStatus {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct() {
//        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        $router = $request->path();
        if (substr($router, 4, 2) === 'wx') {
            $weixinUser = $request->user;
            $user = new Weixin;
            $userId = $user->id($weixinUser);
            if ($userId > 0) {
                $redirectRouter = substr($router, 0, 3) . substr($router, 6);
                $redirectRouter = str_replace('/' . $weixinUser, '/' . $userId, $redirectRouter);
                return redirect($redirectRouter);
            }
        }
        return "no this user";
    }

}
